package com.eightgroups.tces.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.eightgroups.common.result.JsonResult;
import com.eightgroups.common.utils.StringUtil;
import com.eightgroups.tces.entity.Dept;
import com.eightgroups.tces.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import com.eightgroups.common.controller.BaseController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;;import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author zzc2016.cn
 * @since 2017-11-06
 */
@Controller
@RequestMapping("/admin/dept")
public class DeptController extends BaseController {
	@Autowired
    private IDeptService iDeptService;

    /**
     * 系部列表页面
     * @return
     */
	@GetMapping("/list")
    public String list(){
	    return "admin/dept/list";
    }
    /**
     * 获取系部列表
     * @param pageNumber 当前页
     * @param pageSize 每页显示条数
     * @param searchText 搜索名称
     * @return
     */
    @RequestMapping("/getList")
    @ResponseBody
    public Map<String,Object> getList(int pageNumber,int pageSize,String searchText){
        Map<String,Object> result = new HashMap<String,Object>();
        Page<Dept> deptPage = new Page<>(pageNumber,pageSize);
        EntityWrapper<Dept> entityWrapper = new EntityWrapper<>();
        if(StringUtil.isNotEmpty(searchText)){
            entityWrapper.like("dname",searchText);
        }
        Page<Dept> page = iDeptService.selectPage(deptPage, entityWrapper);
        result.put("total", page.getTotal());
        result.put("rows", page.getRecords());
        return result;
    }
    /**
     * 添加系部
     * @param dept
     * @return
     */
    @RequestMapping("/addDept")
    @ResponseBody
    public JsonResult addDept(Dept dept){
        return iDeptService.insert(dept)?renderSuccess("添加成功"):renderError("添加失败");
    }
    /**
     * 修改系部
     * @param dept
     * @return
     */
    @RequestMapping("/updateDept")
    @ResponseBody
    public JsonResult updateDept(Dept dept){
        return iDeptService.updateById(dept)?renderSuccess("修改成功"):renderError("修改失败");
    }
    /**
     * 删除系部
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(@RequestParam(value = "id", required = false) String id) {
        if (StringUtil.isEmpty(id)) {
            return renderError("请选择数据");
        }
        EntityWrapper<Dept> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("id",id);
        return iDeptService.delete(entityWrapper)? renderSuccess("删除成功") : renderError("删除失败");
    }
}
